package com.inter.law.action;

import com.inter.law.dao.IlCaseDAO;
import com.inter.law.dao.IlCaseDynamicDAO;
import com.inter.law.dao.IlLawerDAO;
import com.inter.law.dao.IlTaskDAO;
import com.inter.law.entity.IlCase;
import com.inter.law.entity.IlCaseDynamic;
import com.inter.law.entity.IlLawer;
import com.inter.law.mapper.*;
import com.inter.law.model.*;
import com.inter.law.vo.AddScheduleVO;
import com.inter.law.vo.AddTaskVO;
import com.inter.law.vo.ResultVO;
import com.inter.law.vo.UpdateScheduleVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 登录之后首页的处理逻辑
 */
@Controller
@RequestMapping(value = "/main")
public class MainController {
    @Autowired
    private IlLawerDAO ilLawerDAO;
    @Autowired
    private IlCaseDynamicDAO ilCaseDynamicDAO;
    @Autowired
    private IlCaseDAO ilCaseDAO;
    @Autowired
    private IlTaskDAO ilTaskDAO;
    @Autowired
    private IlScheduleMapper scheduleMapper;
    @Autowired
    private IlCaseDynamicMapper ilCaseDynamicMapper;
    @Autowired
    private IlCalendarMapper calendarMapper;
    @Autowired
    private IlCaseDAO caseDAO;
    @Autowired
    private IlCaseMapper caseMapper;
    @Autowired
    private IlRenwuMapper renwuMapper;
    @Autowired
    private IlRenwuLawerMapper renwuLawerMapper;

    /**
     * 登录之后跳转到的首页
     *
     * @return
     */
    @RequestMapping("/index")
    public ModelAndView init(HttpSession session) {
        ModelAndView modelAndView = new ModelAndView("main/index");
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        List<com.inter.law.model.IlCase> caseList = caseMapper.queryByNum(6, lawerId);
        modelAndView.addObject("caseList",caseList);
        return modelAndView;
    }

    /**
     * 查询所有案件
     *
     * @return
     */
    @RequestMapping(value = "/queryAllCases", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO queryAllCases(HttpSession session) {
        ResultVO resultVO = new ResultVO(true);
        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        //获取案件
        List<com.inter.law.model.IlCase> caseList = caseMapper.queryByNum(Integer.MAX_VALUE, lawerId);

        resultVO.setData(caseList);
        return resultVO;
    }

    /**
     * 查询所有律师
     *
     * @return
     */
    @RequestMapping(value = "/queryAllLawers", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO queryAllLawers(HttpSession session) {
        ResultVO resultVO = new ResultVO(true);
        List all = ilLawerDAO.findAll();
        resultVO.setData(all);
        return resultVO;
    }

    /**
     * 查询律师动态
     *
     * @param session
     * @param lastRecordId 最后一条记录id
     * @param num          每页显示数量
     * @return
     */
    @RequestMapping(value = "/queryDynamicState", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO queryDynamicState(HttpSession session, @RequestParam int lastRecordId, @RequestParam int num) {

        ResultVO resultVO = new ResultVO(true);
        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        //查询律师动态
        List<IlCaseDynamic> ilCaseDynamicList = ilCaseDynamicDAO.findByLawerId(lawerId);                //在il_case_dynamic表中根据lawer_id查找案件动态，找到该律师所有的案件动态
        //按照时间倒序排序
        Collections.sort(ilCaseDynamicList, new Comparator<IlCaseDynamic>() {
            public int compare(IlCaseDynamic o1, IlCaseDynamic o2) {
                return o1.getContentTime().getTime() - o2.getContentTime().getTime() > 0 ? -1 : 1;
            }
        });
        //上一次的动态
        IlCaseDynamic lastDynamic = null;
        int requestNum = 0;

        //获取分页所需动态
        List<IlCaseDynamic> requireDynamicList = new ArrayList<IlCaseDynamic>();
        for (IlCaseDynamic caseDynamic : ilCaseDynamicList) {
            Integer recordId = caseDynamic.getRecordId();
            if (recordId == lastRecordId) {
                //获取上一次的动态
                lastDynamic = caseDynamic;
            }
            if (lastRecordId <= 0) {
                //第一次
                requireDynamicList.add(caseDynamic);
                requestNum++;
            } else {
                //如果在上一次之后 则加入到所需动态列表中
                requireDynamicList.add(caseDynamic);
                if (recordId < lastRecordId) {
                    requestNum++;
                }
            }

            if (requestNum >= num) {
                //数量足够 停止循环
                break;
            }
        }
        //组装数据
        List<Map<String, Object>> dataMapList = new ArrayList<Map<String, Object>>();


        Map<String, Map<String, Object>> tempDataMap = new LinkedHashMap<String, Map<String, Object>>();

        int size = requireDynamicList.size();


        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");

        //获取显示内容
        for (int i = 0; i < size; i++) {
            SimpleDateFormat parentFormat = new SimpleDateFormat("yyyyMMdd");

            IlCaseDynamic caseDynamic = requireDynamicList.get(i);
            //检查是否存在
            String parentKey = parentFormat.format(caseDynamic.getContentTime());
            Map<String, Object> objectMap = tempDataMap.get(parentKey);
            if (objectMap == null) {
                objectMap = new HashMap<String, Object>();
                tempDataMap.put(parentKey, objectMap);
            }
            objectMap.put("parentKey", parentKey);
            //大标题显示日期
            objectMap.put("date", dateFormat.format(caseDynamic.getContentTime()));
            //是否是今天
            objectMap.put("isToday", isSameDay(caseDynamic.getContentTime().getTime(), System.currentTimeMillis()));
            //显示星期
            objectMap.put("weekShow", "周" + getWeekOfDate(caseDynamic.getContentTime()));
            //显示数据集合
            Object dataListObj = objectMap.get("dataList");
            if (dataListObj == null) {
                dataListObj = new ArrayList<Map<String, Object>>();
                objectMap.put("dataList", dataListObj);
            }
            List<Map<String, Object>> dataTempMapList = (List<Map<String, Object>>) dataListObj;

            Map<String, Object> map = new HashMap<String, Object>();
            //id
            map.put("recordId", caseDynamic.getRecordId());
            //显示时间
            map.put("time", timeFormat.format(caseDynamic.getContentTime()));
            //获取律师
            IlLawer lawer = ilLawerDAO.findById(caseDynamic.getLawerId());
            //显示律师名字
            map.put("showContent1", lawer.getLawerName());
            //动态类型
            Integer typeId = caseDynamic.getTypeId();

            //案件id  任务id  日程id
            Integer caseFlagId = caseDynamic.getCaseId();

            if (typeId == 51) {
                //日程
                map.put("flag", 1);
                //创建日程
                IlSchedule schedule = scheduleMapper.selectByPrimaryKey(caseFlagId);
                if (schedule != null) {
                    Integer caseId = schedule.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        } else {
                            IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                            map.put("showContent2", "在日历");
                            map.put("showContent3", ilCalendar.getName());
                        }
                    } else {
                        IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                        map.put("showContent2", "在日历");
                        map.put("showContent3", ilCalendar.getName());
                    }
                    map.put("showContent4", "创建了日程");
                    map.put("showContent5", schedule.getContent());
                }

            } else if (typeId == 54) {
                //完成日程
                //日程
                map.put("flag", 1);
                IlSchedule schedule = scheduleMapper.selectByPrimaryKey(caseFlagId);
                if (schedule != null) {
                    Integer caseId = schedule.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        } else {
                            IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                            map.put("showContent2", "在日历");
                            map.put("showContent3", ilCalendar.getName());
                        }
                    } else {
                        IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                        map.put("showContent2", "在日历");
                        map.put("showContent3", ilCalendar.getName());
                    }
                    map.put("showContent4", "完成了日程");
                    map.put("showContent5", schedule.getContent());
                }
            } else if (typeId == 55) {
                //重新开启日程
                //日程
                map.put("flag", 1);
                IlSchedule schedule = scheduleMapper.selectByPrimaryKey(caseFlagId);
                if (schedule != null) {
                    Integer caseId = schedule.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        } else {
                            IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                            map.put("showContent2", "在日历");
                            map.put("showContent3", ilCalendar.getName());
                        }
                    } else {
                        IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                        map.put("showContent2", "在日历");
                        map.put("showContent3", ilCalendar.getName());
                    }
                    map.put("showContent4", "重新开启了日程");
                    map.put("showContent5", schedule.getContent());
                }
            } else if (typeId == 52) {
                //修改日程
                //日程
                map.put("flag", 1);
                IlSchedule schedule = scheduleMapper.selectByPrimaryKey(caseFlagId);
                if (schedule != null) {
                    Integer caseId = schedule.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        } else {
                            IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                            map.put("showContent2", "在日历");
                            map.put("showContent3", ilCalendar.getName());
                        }
                    } else {
                        IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                        map.put("showContent2", "在日历");
                        map.put("showContent3", ilCalendar.getName());
                    }
                    map.put("showContent4", "修改了日程");
                    map.put("showContent5", schedule.getContent());
                }
            } else if (typeId == 53) {
                //删除日程
                //日程
                map.put("flag", 1);
                IlSchedule schedule = scheduleMapper.selectByPrimaryKey(caseFlagId);
                if (schedule != null) {
                    Integer caseId = schedule.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        } else {
                            IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                            map.put("showContent2", "在日历");
                            map.put("showContent3", ilCalendar.getName());
                        }
                    } else {
                        IlCalendar ilCalendar = calendarMapper.selectByPrimaryKey(schedule.getCalendarId());
                        map.put("showContent2", "在日历");
                        map.put("showContent3", ilCalendar.getName());
                    }
                    map.put("showContent4", "删除了日程");
                    map.put("showContent5", schedule.getContent());
                }
            } else if (typeId == 11) {
                //创建任务
                map.put("flag", 2);
                IlRenwuWithBLOBs renwuWithBLOBs = renwuMapper.selectByPrimaryKey(caseFlagId);
                if (renwuWithBLOBs != null) {
                    Integer caseId = renwuWithBLOBs.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        }
                    }
                    map.put("showContent4", "创建了任务");
                    map.put("showContent5", renwuWithBLOBs.getContent());
                }
            } else if (typeId == 15) {
                //修改任务
                map.put("flag", 2);
                IlRenwuWithBLOBs renwuWithBLOBs = renwuMapper.selectByPrimaryKey(caseFlagId);
                if (renwuWithBLOBs != null) {
                    Integer caseId = renwuWithBLOBs.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        }
                    }
                    map.put("showContent4", "修改了任务");
                    map.put("showContent5", renwuWithBLOBs.getContent());
                }
            } else if (typeId == 16) {
                //删除任务
                map.put("flag", 2);
                IlRenwuWithBLOBs renwuWithBLOBs = renwuMapper.selectByPrimaryKey(caseFlagId);
                if (renwuWithBLOBs != null) {
                    Integer caseId = renwuWithBLOBs.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        }
                    }
                    map.put("showContent4", "删除了任务");
                    map.put("showContent5", renwuWithBLOBs.getContent());
                }
            } else if (typeId == 13) {
                //完成任务
                map.put("flag", 2);
                IlRenwuWithBLOBs renwuWithBLOBs = renwuMapper.selectByPrimaryKey(caseFlagId);
                if (renwuWithBLOBs != null) {
                    Integer caseId = renwuWithBLOBs.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        }
                    }
                    map.put("showContent4", "完成了任务");
                    map.put("showContent5", renwuWithBLOBs.getContent());
                }
            } else if (typeId == 14) {
                //重新开启任务
                map.put("flag", 2);
                IlRenwuWithBLOBs renwuWithBLOBs = renwuMapper.selectByPrimaryKey(caseFlagId);
                if (renwuWithBLOBs != null) {
                    Integer caseId = renwuWithBLOBs.getCaseId();
                    if (caseId != null) {
                        //获取案件
                        IlCase ilCase = ilCaseDAO.findById(caseId);
                        if (ilCase != null) {
                            //案件名字
                            String caseName = ilCase.getCaseName();
                            map.put("showContent2", "在案件");
                            map.put("showContent3", caseName);
                        }
                    }
                    map.put("showContent4", "重新开启了任务");
                    map.put("showContent5", renwuWithBLOBs.getContent());
                }
            }
            dataTempMapList.add(map);

        }

        Set<Map.Entry<String, Map<String, Object>>> entrySet = tempDataMap.entrySet();
        for (Map.Entry<String, Map<String, Object>> entry : entrySet) {
            String key = entry.getKey();
            Map<String, Object> value = entry.getValue();
            dataMapList.add(value);
        }

        resultVO.setData(dataMapList);
        return resultVO;
    }

    /**
     * 查询我的日程
     *
     * @return
     */
    @RequestMapping(value = "/queryMySchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO queryMySchedule(HttpSession session) {
        ResultVO resultVO = new ResultVO(true);
        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        //查询日程
        List<com.inter.law.model.IlSchedule> scheduleList = scheduleMapper.queryIndexShow(lawerId, new Date(), 5);
        //显示数据集合
        List<Map<String, Object>> dataMapList = new ArrayList<Map<String, Object>>();

        for (com.inter.law.model.IlSchedule schedule : scheduleList) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", schedule.getId());
            map.put("content", schedule.getContent());
            map.put("beginShow", getShowTime(schedule.getBegin()));
            map.put("endShow", getShowTime(schedule.getEnd()));
            dataMapList.add(map);
        }


        resultVO.setData(dataMapList);
        return resultVO;
    }

    /**
     * 完成日程
     *
     * @param scheduleId 日程id
     * @return
     */
    @RequestMapping(value = "/completeSchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO completeSchedule(@RequestParam int scheduleId, HttpSession session) {
        ResultVO resultVO = new ResultVO(true);

        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        IlSchedule ilSchedule = new IlSchedule();
        ilSchedule.setId(scheduleId);
        ilSchedule.setFlag(2);
        scheduleMapper.updateByPrimaryKeySelective(ilSchedule);
        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(scheduleId);
        dynamic.setContent("");
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(54);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 重新打开日程
     *
     * @param scheduleId 日程id
     * @return
     */
    @RequestMapping(value = "/openSchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO openSchedule(@RequestParam int scheduleId, HttpSession session) {
        ResultVO resultVO = new ResultVO(true);
        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        IlSchedule ilSchedule = new IlSchedule();
        ilSchedule.setId(scheduleId);
        ilSchedule.setFlag(1);
        scheduleMapper.updateByPrimaryKeySelective(ilSchedule);
        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(scheduleId);
        dynamic.setContent("");
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(55);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 删除日程
     *
     * @param scheduleId 日程id
     * @return
     */
    @RequestMapping(value = "/deleteSchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO deleteSchedule(@RequestParam int scheduleId, HttpSession session) {
        ResultVO resultVO = new ResultVO(true);

        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        IlSchedule ilSchedule = new IlSchedule();
        ilSchedule.setId(scheduleId);
        ilSchedule.setIsDelete(1);
        scheduleMapper.updateByPrimaryKeySelective(ilSchedule);
        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(scheduleId);
        dynamic.setContent("");
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(53);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 创建日程
     *
     * @param session
     * @param addScheduleVO
     * @return
     */
    @RequestMapping(value = "/addSchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO addSchedule(HttpSession session, @ModelAttribute AddScheduleVO addScheduleVO) throws ParseException {

        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        ResultVO resultVO = new ResultVO(true);
        IlSchedule schedule = new IlSchedule();
        schedule.setIsDelete(0);
        schedule.setFlag(1);
        schedule.setContent(addScheduleVO.getContent());
        schedule.setAddress(addScheduleVO.getAddress());
        schedule.setAlertType(addScheduleVO.getAlertType());
        schedule.setCalendarId(1);
        schedule.setCaseId(addScheduleVO.getCaseId());
        schedule.setCreateTime(new Date());
        schedule.setCreateId(lawerId);
        schedule.setIsAlert(1);
        schedule.setCalendarId(1);
        schedule.setIsPrivate(1);
        schedule.setRetryTime(addScheduleVO.getRetryTime());
        schedule.setPhone(addScheduleVO.getPhone());

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
        String beginDate = addScheduleVO.getBeginDate();
        String beginTime = addScheduleVO.getBeginTime();
        if (StringUtils.isBlank(beginDate)) {
            beginDate = dformat.format(new Date());
        }
        if (StringUtils.isBlank(beginTime)) {
            beginTime = "09:00";
        }
        schedule.setBegin(format.parse(beginDate + " " + beginTime));
        String endDate = addScheduleVO.getEndDate();
        String endTime = addScheduleVO.getEndTime();
        if (StringUtils.isBlank(endDate)) {
            endDate = dformat.format(new Date());
        }
        if (StringUtils.isBlank(endTime)) {
            endTime = "17:00";
        }
        schedule.setEnd(format.parse(endDate + " " + endTime));

        scheduleMapper.insertSelective(schedule);

        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(schedule.getId());
        dynamic.setContent(schedule.getContent());
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(51);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 修改日程
     *
     * @param session
     * @param updateScheduleVO
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/updateSchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO updateSchedule(HttpSession session, @ModelAttribute UpdateScheduleVO updateScheduleVO) throws ParseException {

        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        ResultVO resultVO = new ResultVO(true);
        IlSchedule schedule = new IlSchedule();
        schedule.setIsDelete(0);
        schedule.setFlag(1);
        schedule.setContent(updateScheduleVO.getContent());
        schedule.setAddress(updateScheduleVO.getAddress());
        schedule.setAlertType(updateScheduleVO.getAlertType());
        schedule.setCalendarId(1);
        schedule.setCaseId(updateScheduleVO.getCaseId());
        schedule.setCreateTime(new Date());
        schedule.setCreateId(lawerId);
        schedule.setIsAlert(1);
        schedule.setCalendarId(1);
        schedule.setIsPrivate(1);
        schedule.setRetryTime(updateScheduleVO.getRetryTime());
        schedule.setPhone(updateScheduleVO.getPhone());

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
        String beginDate = updateScheduleVO.getBeginDate();
        String beginTime = updateScheduleVO.getBeginTime();
        if (StringUtils.isBlank(beginDate)) {
            beginDate = dformat.format(new Date());
        }
        if (StringUtils.isBlank(beginTime)) {
            beginTime = "09:00";
        }
        schedule.setBegin(format.parse(beginDate + " " + beginTime));
        String endDate = updateScheduleVO.getEndDate();
        String endTime = updateScheduleVO.getEndTime();
        if (StringUtils.isBlank(endDate)) {
            endDate = dformat.format(new Date());
        }
        if (StringUtils.isBlank(endTime)) {
            endTime = "17:00";
        }
        schedule.setEnd(format.parse(endDate + " " + endTime));
        schedule.setId(updateScheduleVO.getId());
        scheduleMapper.updateByPrimaryKeySelective(schedule);

        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(schedule.getId());
        dynamic.setContent(schedule.getContent());
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(52);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 创建任务
     *
     * @param session
     * @param addTaskVO
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/addTask", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO addTask(HttpSession session, @ModelAttribute AddTaskVO addTaskVO) throws ParseException {

        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        ResultVO resultVO = new ResultVO(true);


        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd");
        String deadline = addTaskVO.getDeadline();


        IlRenwuWithBLOBs renwuWithBLOBs = new IlRenwuWithBLOBs();
        renwuWithBLOBs.setCreateTime(new Date());
        renwuWithBLOBs.setContent(addTaskVO.getContent());
        renwuWithBLOBs.setDescription(addTaskVO.getDescription());
        renwuWithBLOBs.setCaseId(addTaskVO.getCaseId());
        renwuWithBLOBs.setIsDelete(0);
        renwuWithBLOBs.setStars(addTaskVO.getStars());
        renwuWithBLOBs.setLawerId(lawerId);
        if (StringUtils.isNotBlank(deadline)) {
            renwuWithBLOBs.setDeadline(dformat.parse(deadline));
        }
        renwuMapper.insert(renwuWithBLOBs);
        IlRenwuLawer ilRenwuLawer = new IlRenwuLawer();
        ilRenwuLawer.setLawerId(addTaskVO.getLawerId());
        ilRenwuLawer.setIsComplete(0);
        ilRenwuLawer.setRenwuId(renwuWithBLOBs.getId());
        renwuLawerMapper.insertSelective(ilRenwuLawer);

        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(renwuWithBLOBs.getId());
        dynamic.setContent(renwuWithBLOBs.getContent());
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(11);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 删除任务
     *
     * @param taskId 任务id
     * @return
     */
    @RequestMapping(value = "/deleteTask", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO deleteTask(@RequestParam int taskId, HttpSession session) {
        ResultVO resultVO = new ResultVO(true);

        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        IlRenwu renwu = new IlRenwu();
        renwu.setId(taskId);
        renwu.setIsDelete(1);
        renwuMapper.updateByPrimaryKey(renwu);
        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(taskId);
        dynamic.setContent("");
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(16);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 完成任务
     *
     * @param taskId 任务id
     * @return
     */
    @RequestMapping(value = "/completeTask", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO completeTask(@RequestParam int taskId, HttpSession session) {
        ResultVO resultVO = new ResultVO(true);

        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        IlRenwuLawer renwuLawer = new IlRenwuLawer();
        renwuLawer.setId(taskId);
        renwuLawer.setIsComplete(1);
        renwuLawer.setCompleteTime(new Date());
        renwuLawerMapper.updateByPrimaryKeySelective(renwuLawer);
        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(taskId);
        dynamic.setContent("");
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(13);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 重新打开任务
     *
     * @param taskId 任务id
     * @return
     */
    @RequestMapping(value = "/openTask", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO openTask(@RequestParam int taskId, HttpSession session) {
        ResultVO resultVO = new ResultVO(true);

        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);

        IlRenwuLawer renwuLawer = new IlRenwuLawer();
        renwuLawer.setId(taskId);
        renwuLawer.setIsComplete(0);
        renwuLawerMapper.updateByPrimaryKeySelective(renwuLawer);
        //更新动态
        com.inter.law.model.IlCaseDynamic dynamic = new com.inter.law.model.IlCaseDynamic();
        dynamic.setCaseId(taskId);
        dynamic.setContent("");
        dynamic.setContentPara("");
        dynamic.setContentTime(new Date());
        dynamic.setTypeId(14);
        dynamic.setLawerId(lawerId);
        ilCaseDynamicMapper.insertSelective(dynamic);
        return resultVO;
    }

    /**
     * 获取分配给我的任务
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/getToMeTask", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO getToMeTask(HttpSession session) {


        ResultVO resultVO = new ResultVO(true);
        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        //查询我的任务
        List<Map<String, Object>> mapList = renwuLawerMapper.getMyTask(lawerId, new Date());
        for (Map<String, Object> m : mapList) {
            Object deadline = m.get("deadline");
            if (deadline != null) {
                Timestamp timestamp = (Timestamp) deadline;
                m.put("deadLineShow", getShowDate(timestamp));
            }
            Object create_time = m.get("create_time");
            if (create_time != null) {
                Timestamp timestamp = (Timestamp) create_time;
                m.put("createTimeShow", getShowTime(timestamp));
            }
        }

        Map<String, Object> map = new HashMap<String, Object>();

        int size = mapList.size();
        map.put("size", size);
        map.put("dataList", size <= 5 ? mapList : mapList.subList(0, 5));

        resultVO.setData(map);
        return resultVO;
    }

    /**
     * 获取我创建的的任务
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "/getMeCreateTask", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO getMeCreateTask(HttpSession session) {


        ResultVO resultVO = new ResultVO(true);
        //获取id
        String lawerIdString = session.getAttribute("id").toString();
        int lawerId = Integer.parseInt(lawerIdString);
        List<Map<String, Object>> mapList = new ArrayList<Map<String, Object>>();
        //查询我的任务
        List<IlRenwuWithBLOBs> renwuWithBLOBsList = renwuMapper.getMyCreateTask(lawerId, new Date());
        for (IlRenwuWithBLOBs renwu : renwuWithBLOBsList) {

            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", renwu.getId());
            map.put("content", renwu.getContent());
            map.put("stars", renwu.getStars());

            Date deadline = renwu.getDeadline();
            if (deadline != null) {
                map.put("deadLineShow", getShowDate(deadline));
            }
            Date createTime = renwu.getCreateTime();
            if (createTime != null) {
                map.put("createTimeShow", getShowTime(createTime));
            }
            List<Map<String, Object>> renwuMapList = new ArrayList<Map<String, Object>>();
            List<IlRenwuLawer> renwuLawerList = renwuLawerMapper.getByRenwuId(renwu.getId());
            for (IlRenwuLawer renwuLawer : renwuLawerList) {
                Map<String, Object> m=new HashMap<String, Object>();
                IlLawer lawer = ilLawerDAO.findById(renwuLawer.getLawerId());
                m.put("name",lawer.getLawerName());
                renwuMapList.add(m);
            }
            map.put("renwuren",renwuMapList);
            mapList.add(map);
        }

        Map<String, Object> map = new HashMap<String, Object>();

        int size = renwuWithBLOBsList.size();
        map.put("size", size);
        map.put("dataList", size <= 5 ? mapList : mapList.subList(0, 5));

        resultVO.setData(map);
        return resultVO;
    }

    /**
     * 改变任务星级
     *
     * @return
     */
    @RequestMapping(value = "/updateTaskStars", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO updateTaskStars(@RequestParam  int taskId,@RequestParam int num) {


        ResultVO resultVO = new ResultVO(true);
        IlRenwuWithBLOBs renwuWithBLOBs = new IlRenwuWithBLOBs();
        renwuWithBLOBs.setId(taskId);
        renwuWithBLOBs.setStars(num);
        renwuMapper.updateByPrimaryKeySelective(renwuWithBLOBs);

        return resultVO;
    }

    /**
     * 根据日程id获取日程
     *
     * @param scheduleId 日程id
     * @return
     * @throws ParseException
     */
    @RequestMapping(value = "/getSchedule", method = RequestMethod.POST)
    public
    @ResponseBody
    ResultVO getSchedule(@RequestParam int scheduleId) throws ParseException {
        ResultVO resultVO = new ResultVO(true);
        IlSchedule ilSchedule = scheduleMapper.selectByPrimaryKey(scheduleId);

        AddScheduleVO addScheduleVO = getAddScheduleVOFromIlSchedule(ilSchedule);


        resultVO.setData(addScheduleVO);
        return resultVO;
    }

    /**
     * 将IlSchedule转为AddScheduleVO
     *
     * @param schedule
     * @return
     */
    public static AddScheduleVO getAddScheduleVOFromIlSchedule(IlSchedule schedule) {
        if (schedule == null) {
            return null;
        }
        AddScheduleVO addScheduleVO = new AddScheduleVO();
        addScheduleVO.setCaseId(schedule.getCaseId());
        addScheduleVO.setRetryTime(schedule.getRetryTime());
        addScheduleVO.setAlertType(schedule.getAlertType());
        addScheduleVO.setPhone(schedule.getPhone());
        addScheduleVO.setAddress(schedule.getAddress());
        addScheduleVO.setContent(schedule.getContent());

        SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat format2 = new SimpleDateFormat("HH:mm");
        addScheduleVO.setBeginDate(format1.format(schedule.getBegin()));
        addScheduleVO.setBeginTime(format2.format(schedule.getBegin()));
        addScheduleVO.setEndDate(format1.format(schedule.getEnd()));
        addScheduleVO.setEndTime(format2.format(schedule.getEnd()));

        return addScheduleVO;
    }

    /**
     * 获取显示日期
     *
     * @param date
     * @return
     */
    public static String getShowTime(Date date) {
        Date now = new Date();

        int nowYear = now.getYear();
        int nowMonth = now.getMonth();
        int nowDay = now.getDate();

        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDate();


        if (nowYear == year && nowMonth == month && nowDay == day) {
            //同一天
            SimpleDateFormat format = new SimpleDateFormat("HH:mm");
            return format.format(date);
        } else if (nowYear == year && nowMonth == month) {
            //同一个月
            SimpleDateFormat format = new SimpleDateFormat("dd HH:mm");
            return format.format(date);
        } else if (nowYear == year) {
            //同一年
            SimpleDateFormat format = new SimpleDateFormat("MM-dd HH:mm");
            return format.format(date);
        } else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            return format.format(date);
        }

    }

    public static String getShowDate(Date date) {
        Date now = new Date();

        int nowYear = now.getYear();
        int nowMonth = now.getMonth();
        int nowDay = now.getDate();

        int year = date.getYear();
        int month = date.getMonth();
        int day = date.getDate();


        SimpleDateFormat format = new SimpleDateFormat("MM-dd");
        if (nowYear == year && nowMonth == month && nowDay == day) {
            //同一天
            return format.format(date);
        } else if (nowYear == year && nowMonth == month) {
            //同一个月
            return format.format(date);
        } else if (nowYear == year) {
            //同一年
            return format.format(date);
        } else {
            SimpleDateFormat sformat = new SimpleDateFormat("yyyy-MM-dd");
            return sformat.format(date);
        }

    }

    /**
     * 检查两个时间是否在同一天
     *
     * @param timestamp1
     * @param timestamp2
     * @return
     */
    public static boolean isSameDay(long timestamp1, long timestamp2) {

        Date date1 = new Date(timestamp1);
        Date date2 = new Date(timestamp2);


        if (date1.getYear() == date2.getYear() && date1.getMonth() == date2.getMonth() && date1.getDate() == date2.getDate()) {
            //同一天 不用显示
            return true;
        }
        return false;
    }

    /**
     * 获取星期
     *
     * @param dt
     * @return
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"日", "一", "二", "三", "四", "五", "六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);
        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;
        return weekDays[w];
    }


}
